#include <iostream>
#include <atomic>
#include <string>

class DoubleBuffer
{
public:
    DoubleBuffer() {
        _used_index = 0;
        _records[0] = "init data";
    }
    ~DoubleBuffer() {
    }

    void update() {
        uint32_t unused_index = 1 - _used_index;
        _records[unused_index] = "new data";
        _used_index = unused_index;
    }

    std::string get() const {
        return _records[_used_index];
    }

private:
    std::atomic<uint32_t> _used_index; //原子变量
    std::string _records[2];
};